﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using Memorandum.DBContext;
using Memorandum.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace Memorandum.ViewModel
{
    public class RemindListViewModel : ViewModelBase
    {

        private ObservableCollection<RemindItem> remindSet = new ObservableCollection<RemindItem>();

        public ObservableCollection<RemindItem> RemindSet
        {
            get { return remindSet; }
        }

        public RemindListViewModel()
        {

            refresh();
        }
        private void refresh()
        {
            Task.Run(() =>
            {
                lock (App.objlock)
                {
                    var items = SqliteDBContext.Instance.RemindItems;
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        remindSet.Clear();
                        foreach (var item in items)
                        {
                            remindSet.Add(item);
                        }

                    });

                    //List<RemindItem> lst = new List<RemindItem>();

                    //using (SqliteDBContext db = new SqliteDBContext())
                    //{
                    //    foreach (var item in db.RemindItems)
                    //    {
                    //        lst.Add(item);
                    //    }
                    //}
                    //Application.Current.Dispatcher.Invoke(() =>
                    //{
                    //    foreach (var item in lst)
                    //    {
                    //        remindSet.Add(item);
                    //    }
                    //});
                    Messenger.Default.Send("Message", Message.HiddenWait);
               }        
            });
        }

        private RemindItem selectedModel;
        public RemindItem SelectedModel
        {
            set
            {
                Set(ref selectedModel, value);
            }
            get
            {
                return selectedModel;
            }
        }
        //private ICommand deleteCmd;
        //public ICommand DeleteCmd
        //{
        //    get
        //    {
        //        if (deleteCmd == null)
        //        {
        //            deleteCmd = new RelayCommand(() =>
        //            {
        //                if (selectedModel != null)
        //                {

        //                }

        //            });
        //        }
        //        return deleteCmd;
        //    }
        //}

        private RelayCommand<string> deleteCmd;

        public RelayCommand<string> DeleteCmd
        {
            get
            {
                if (deleteCmd == null)
                {
                    deleteCmd = new RelayCommand<string>(AddUser, (string p) => { return true; });
                }
                return deleteCmd;
            }
            set { deleteCmd = value; }
        }
        private void AddUser(string par)
        {
            var item = RemindSet.First(c => c.ID == int.Parse(par));
            if (item != null)
            {
                SqliteDBContext.Instance.RemindItems.Remove(item);
                SqliteDBContext.Instance.SaveChanges();
            }

            Messenger.Default.Send("Message", Message.ShowSuccess);
            refresh();

        }


    }
}
